package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.IEntity;
import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;










public class DefaultSaxParser
  extends DefaultHandler
{
  private DefaultLinker m_linker = new DefaultLinker(true);
  
/*  38 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  40 */   private Stack<String> m_tags = new Stack();

/*  42 */   private Stack<Object> m_objs = new Stack();
  
  private IEntity<?> m_entity;
  
/*  46 */   private StringBuilder m_text = new StringBuilder();


/*  49 */   public static MonitorRules parse(InputStream in) throws SAXException, IOException { return (MonitorRules)parseEntity(MonitorRules.class, new InputSource((InputStream)removeBOM(in))); }



/*  53 */   public static MonitorRules parse(Reader reader) throws SAXException, IOException { return (MonitorRules)parseEntity(MonitorRules.class, new InputSource((Reader)removeBOM(reader))); }



/*  57 */   public static MonitorRules parse(String xml) throws SAXException, IOException { return (MonitorRules)parseEntity(MonitorRules.class, new InputSource(new StringReader((String)removeBOM(xml)))); }


  
  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  63 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  64 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  66 */       factory.setValidating(false);
/*  67 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  69 */       factory.newSAXParser().parse(is, handler);
/*  70 */       return (T)handler.getEntity();
/*  71 */     } catch (ParserConfigurationException e) {
/*  72 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }

  
/*  77 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  81 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }



  
  private static <T> T removeBOM(T obj) throws IOException {
/*  87 */     if (obj instanceof String) {
/*  88 */       String str = (String)obj;

/*  90 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  91 */         return (T)str.substring(1);
      }
/*  93 */       return obj;
    }
/*  95 */     if (obj instanceof InputStream) {
/*  96 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  98 */       in.mark(3);

/* 100 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 101 */         in.reset();
      }
      
/* 104 */       return (T)in;
/* 105 */     }  if (obj instanceof Reader) {
/* 106 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 108 */       in.mark(1);

/* 110 */       if (in.read() != 65279) {
/* 111 */         in.reset();
      }
      
/* 114 */       return (T)in;
    }
/* 116 */     return obj;
  }


  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 122 */     if (value == null || value.length() == 0) {
/* 123 */       return defaultValue;
    }
    
/* 126 */     if (type == Boolean.class)
/* 127 */       return (T)Boolean.valueOf(value); 
/* 128 */     if (type == Integer.class)
/* 129 */       return (T)Integer.valueOf(value); 
/* 130 */     if (type == Long.class)
/* 131 */       return (T)Long.valueOf(value); 
/* 132 */     if (type == Short.class)
/* 133 */       return (T)Short.valueOf(value); 
/* 134 */     if (type == Float.class)
/* 135 */       return (T)Float.valueOf(value); 
/* 136 */     if (type == Double.class)
/* 137 */       return (T)Double.valueOf(value); 
/* 138 */     if (type == Byte.class)
/* 139 */       return (T)Byte.valueOf(value); 
/* 140 */     if (type == Character.class) {
/* 141 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 143 */     return (T)value;
  }



  
/* 149 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 154 */   public void endDocument() { this.m_linker.finish(); }


  
  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 159 */     if (uri == null || uri.length() == 0) {
/* 160 */       this.m_objs.pop();
/* 161 */       this.m_tags.pop();
    }

    
/* 165 */     this.m_text.setLength(0);
  }

  
/* 169 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 173 */   protected String getText() { return this.m_text.toString(); }

  
  private void parseForCondition(Condition parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 177 */     if ("sub-condition".equals(qName)) {
/* 178 */       SubCondition subCondition = this.m_maker.buildSubCondition(attributes);

/* 180 */       this.m_linker.onSubCondition(parentObj, subCondition);
/* 181 */       this.m_objs.push(subCondition);
    } else {
/* 183 */       throw new SAXException(String.format("Element(%s) is not expected under condition!", new Object[] { qName }));
    }
    
/* 186 */     this.m_tags.push(qName);
  }
  
  private void parseForConfig(Config parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 190 */     if ("condition".equals(qName)) {
/* 191 */       Condition condition = this.m_maker.buildCondition(attributes);

/* 193 */       this.m_linker.onCondition(parentObj, condition);
/* 194 */       this.m_objs.push(condition);
    } else {
/* 196 */       throw new SAXException(String.format("Element(%s) is not expected under config!", new Object[] { qName }));
    }
    
/* 199 */     this.m_tags.push(qName);
  }
  
  private void parseForMetricItem(MetricItem parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 203 */     this.m_objs.push(parentObj);
/* 204 */     this.m_tags.push(qName);
  }
  
  private void parseForMonitorRules(MonitorRules parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 208 */     if ("rule".equals(qName)) {
/* 209 */       Rule rule = this.m_maker.buildRule(attributes);

/* 211 */       this.m_linker.onRule(parentObj, rule);
/* 212 */       this.m_objs.push(rule);
    } else {
/* 214 */       throw new SAXException(String.format("Element(%s) is not expected under monitor-rules!", new Object[] { qName }));
    }
    
/* 217 */     this.m_tags.push(qName);
  }
  
  private void parseForRule(Rule parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 221 */     if ("metric-item".equals(qName)) {
/* 222 */       MetricItem metricItem = this.m_maker.buildMetricItem(attributes);

/* 224 */       this.m_linker.onMetricItem(parentObj, metricItem);
/* 225 */       this.m_objs.push(metricItem);
/* 226 */     } else if ("config".equals(qName)) {
/* 227 */       Config config = this.m_maker.buildConfig(attributes);

/* 229 */       this.m_linker.onConfig(parentObj, config);
/* 230 */       this.m_objs.push(config);
    } else {
/* 232 */       throw new SAXException(String.format("Element(%s) is not expected under rule!", new Object[] { qName }));
    }
    
/* 235 */     this.m_tags.push(qName);
  }
  
  private void parseForSubCondition(SubCondition parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 239 */     this.m_objs.push(parentObj);
/* 240 */     this.m_tags.push(qName);
  }
  
  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 244 */     if ("monitor-rules".equals(qName)) {
/* 245 */       MonitorRules monitorRules = this.m_maker.buildMonitorRules(attributes);

/* 247 */       this.m_entity = monitorRules;
/* 248 */       this.m_objs.push(monitorRules);
/* 249 */       this.m_tags.push(qName);
/* 250 */     } else if ("rule".equals(qName)) {
/* 251 */       Rule rule = this.m_maker.buildRule(attributes);

/* 253 */       this.m_entity = rule;
/* 254 */       this.m_objs.push(rule);
/* 255 */       this.m_tags.push(qName);
/* 256 */     } else if ("metric-item".equals(qName)) {
/* 257 */       MetricItem metricItem = this.m_maker.buildMetricItem(attributes);

/* 259 */       this.m_entity = metricItem;
/* 260 */       this.m_objs.push(metricItem);
/* 261 */       this.m_tags.push(qName);
/* 262 */     } else if ("config".equals(qName)) {
/* 263 */       Config config = this.m_maker.buildConfig(attributes);

/* 265 */       this.m_entity = config;
/* 266 */       this.m_objs.push(config);
/* 267 */       this.m_tags.push(qName);
/* 268 */     } else if ("condition".equals(qName)) {
/* 269 */       Condition condition = this.m_maker.buildCondition(attributes);

/* 271 */       this.m_entity = condition;
/* 272 */       this.m_objs.push(condition);
/* 273 */       this.m_tags.push(qName);
/* 274 */     } else if ("sub-condition".equals(qName)) {
/* 275 */       SubCondition subCondition = this.m_maker.buildSubCondition(attributes);

/* 277 */       this.m_entity = subCondition;
/* 278 */       this.m_objs.push(subCondition);
/* 279 */       this.m_tags.push(qName);
    } else {
/* 281 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }

  
  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 287 */     if (uri == null || uri.length() == 0) {
/* 288 */       if (this.m_objs.isEmpty()) {
/* 289 */         parseRoot(qName, attributes);
      } else {
/* 291 */         Object parent = this.m_objs.peek();
/* 292 */         String tag = (String)this.m_tags.peek();

/* 294 */         if (parent instanceof MonitorRules) {
/* 295 */           parseForMonitorRules((MonitorRules)parent, tag, qName, attributes);
/* 296 */         } else if (parent instanceof Rule) {
/* 297 */           parseForRule((Rule)parent, tag, qName, attributes);
/* 298 */         } else if (parent instanceof MetricItem) {
/* 299 */           parseForMetricItem((MetricItem)parent, tag, qName, attributes);
/* 300 */         } else if (parent instanceof Config) {
/* 301 */           parseForConfig((Config)parent, tag, qName, attributes);
/* 302 */         } else if (parent instanceof Condition) {
/* 303 */           parseForCondition((Condition)parent, tag, qName, attributes);
/* 304 */         } else if (parent instanceof SubCondition) {
/* 305 */           parseForSubCondition((SubCondition)parent, tag, qName, attributes);
        } else {
/* 307 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }
      
/* 311 */       this.m_text.setLength(0);
    } else {
/* 313 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */